package com.example.soa_exp2.controller;

import com.example.soa_exp2.common.EquipmentModel;
import com.example.soa_exp2.common.MessageModel;
import com.example.soa_exp2.common.RecordModel;
import com.example.soa_exp2.common.UserModel;
import com.example.soa_exp2.domain.BorrowRecord;
import com.example.soa_exp2.domain.Equipment;
import com.example.soa_exp2.domain.User;
import com.example.soa_exp2.persistence.RecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.CollectionModel;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

@RestController
public class RecordController {

    @Autowired
    private RecordMapper recordMapper;

    @GetMapping("/record")
    public HttpEntity<CollectionModel<RecordModel>> getAllRecord() {
        List<BorrowRecord> recordList = recordMapper.selectList(null);
        List<RecordModel> recordModels = new ArrayList<>();
        for(BorrowRecord record:recordList){
            RecordModel model = new RecordModel(record);
            model.add(linkTo(methodOn(RecordController.class).getRecordById(record.getRecordid())).withSelfRel());
            recordModels.add(model);
        }
        CollectionModel<RecordModel> models = CollectionModel.of(recordModels);
        models.add(linkTo(methodOn(RecordController.class).getAllRecord()).withSelfRel());
        return new ResponseEntity<>(models, HttpStatus.OK);
    }

    @GetMapping("/record/{recordId}")
    public HttpEntity<RecordModel> getRecordById(@PathVariable String recordId){
        BorrowRecord record = recordMapper.selectById(recordId);
        if(record == null){
            RecordModel model = new RecordModel(null, "没有查询到记录");
            model.add(linkTo(methodOn(RecordController.class).getAllRecord()).withRel("allRecord"));
            return new ResponseEntity<>(model, HttpStatus.OK);
        }else {
            RecordModel model = new RecordModel(record);
            model.add(linkTo(methodOn(RecordController.class).getRecordById(recordId)).withSelfRel());
            model.add(linkTo(methodOn(UserController.class).getUserById(record.getUserid())).withRel("User"));
            model.add(linkTo(methodOn(EquipmentController.class).getEquipmentById(record.getEquipmentid())).withRel("Equipment"));
            return new ResponseEntity<>(model, HttpStatus.OK);
        }
    }

    @PostMapping("/record")
    public HttpEntity<RecordModel> addRecord(@RequestBody BorrowRecord addRecord){
        recordMapper.insert(addRecord);
        RecordModel model = new RecordModel(addRecord);
        model.add(linkTo(methodOn(RecordController.class).addRecord(addRecord)).withSelfRel());
        model.add(linkTo(methodOn(UserController.class).getUserById(addRecord.getUserid())).withRel("User"));
        model.add(linkTo(methodOn(EquipmentController.class).getEquipmentById(addRecord.getEquipmentid())).withRel("Equipment"));
        return new ResponseEntity<>(model, HttpStatus.OK);
    }

    @PutMapping("/record/{recordId}")
    public HttpEntity<RecordModel> updateRecordById(@PathVariable String recordId, @RequestBody BorrowRecord newRecord){
        newRecord.setRecordid(recordId);
        recordMapper.updateById(newRecord);
        RecordModel model = new RecordModel(newRecord);
        model.add(linkTo(methodOn(RecordController.class).updateRecordById(recordId, newRecord)).withSelfRel());
        model.add(linkTo(methodOn(UserController.class).getUserById(newRecord.getUserid())).withRel("User"));
        model.add(linkTo(methodOn(EquipmentController.class).getEquipmentById(newRecord.getEquipmentid())).withRel("Equipment"));
        return new ResponseEntity<>(model, HttpStatus.OK);
    }

    @DeleteMapping("/record/{recordId}")
    public HttpEntity<MessageModel> deleteRecordById(@PathVariable String recordId){
        recordMapper.deleteById(recordId);
        MessageModel model = new MessageModel("删除成功！");
        model.add(linkTo(methodOn(UserController.class).getAllUser()).withRel("allUser"));
        return new ResponseEntity<>(model, HttpStatus.OK);
    }
}

